package com.tdd.api.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tdd.api.server.domain.param.wx.app.UserInfoParam;
import com.tdd.api.server.domain.param.wx.app.UserOpenIdParam;
import com.tdd.api.server.domain.param.wx.app.UserPushParam;
import com.tdd.api.server.domain.param.wx.app.UserTokenParam;
import com.tdd.api.server.entity.TbUserOpen;
import com.tdd.api.server.dao.TbUserOpenMapper;
import com.tdd.api.server.service.ITbUserOpenService;
import com.tdd.api.server.service.IWeChatService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TDD
 * @since 2025-02-11
 */
@Service
public class TbUserOpenServiceImpl extends ServiceImpl<TbUserOpenMapper, TbUserOpen> implements ITbUserOpenService {

    @Resource
    private IWeChatService weChatService;

    @Override
    public void saveUser(UserInfoParam param) {

        if (StrUtil.isNotEmpty(param.getUserId()) && StrUtil.isNotEmpty(param.getCode()) && StrUtil.isNotEmpty(param.getToken())) {
            String openId = weChatService.getOpenid(param.getCode());
            TbUserOpen o = selectByUserId(param.getUserId());
            if (o == null) {
                o = BeanUtil.toBean(param, TbUserOpen.class);
                o.setOpenId(openId);

            } else {
                o.setOpenId(openId);
                o.setToken(param.getToken());
                o.setUsername(param.getUsername());
            }
            if (o.getId() != null) {
                updateById(o);
            } else {
                save(o);
            }

        }
    }

    @Override
    public void saveOpenId(UserOpenIdParam param) {
        if (StrUtil.isNotEmpty(param.getUserId()) && StrUtil.isNotEmpty(param.getCode())) {
            String openId = weChatService.getOpenid(param.getCode());
            TbUserOpen o = selectByUserId(param.getUserId());
            if (o == null) {
                o = BeanUtil.toBean(param, TbUserOpen.class);
                o.setOpenId(openId);
            } else {
                o.setOpenId(openId);
                o.setUsername(o.getUsername());
            }
            save(o);
        }
    }

    @Override
    public void saveToken(UserTokenParam param) {
        if (StrUtil.isNotEmpty(param.getUserId()) && StrUtil.isNotEmpty(param.getToken())) {
            TbUserOpen o = selectByUserId(param.getUserId());
            if (o == null) {
                o = BeanUtil.toBean(param, TbUserOpen.class);
                o.setToken(param.getToken());
            } else {
                o.setToken(param.getToken());
                o.setUsername(o.getUsername());
            }
            save(o);
        }
    }

    @Override
    public void pushEnable(UserPushParam param) {
        TbUserOpen user = selectByUserId(param.getUserId());
        if (user != null) {
            user.setPushStatus(param.getPushStatus());
            user.setPushTime(LocalDateTime.now());
            updateById(user);
        }
    }

    @Override
    public TbUserOpen selectByUserId(String userId) {
        LambdaQueryWrapper<TbUserOpen> qry = new LambdaQueryWrapper<>();
        qry.eq(TbUserOpen::getUserId, userId)
                .orderByDesc(TbUserOpen::getCreatedTime)
                .last("limit 1");
        return getOne(qry);
    }

    @Override
    public List<TbUserOpen> pushUser() {
        LambdaQueryWrapper<TbUserOpen> qry = new LambdaQueryWrapper<>();
        qry.eq(TbUserOpen::getPushStatus, 1);
        return baseMapper.selectList(qry);
    }


}
